package com.xiaoye.qbproject.controller.qbzyk;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.ZbModule;
import com.xiaoye.qbproject.page.TableDataInfo;
import com.xiaoye.qbproject.service.ZbModuleService;
import com.xiaoye.qbproject.util.StringUtils;
import com.xiaoye.qbproject.vo.SetZbInfoAndZbModuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ChenYao
 * @version : 1.0
 * @description : 装备组件
 * @create : 2024-12-27 3:10
 */
@RestController
@RequestMapping("/zbModule")
public class ZbModuleController extends BaseController {
    @Autowired
    private ZbModuleService zbModuleService;

    /**
     * @description: 列表查询
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.page.TableDataInfo
     * @ceateDate: 2024-12-28 1:49
     */

    @GetMapping("/list")
    public TableDataInfo getZbInfoList(ZbModule zbModule) {
        startPage();
        String name = zbModule.getName();
        String parameter = zbModule.getParameter();
        String zbInfoId = zbModule.getZbInfoId();

        LambdaQueryWrapper<ZbModule> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(ZbModule::getName, name);
        }
        if (StringUtils.isNotBlank(parameter)) {
            queryWrapper.like(ZbModule::getParameter, parameter);
        }
        if (StringUtils.isNotBlank(zbInfoId)) {
            queryWrapper.eq(ZbModule::getZbInfoId, zbInfoId);
        }

        List<ZbModule> list = zbModuleService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping("/dropDown")
    public AjaxResult getAllZbInfo(ZbModule zbModule) {
        LambdaQueryWrapper<ZbModule> queryWrapper = Wrappers.lambdaQuery();
        String zbInfoId = zbModule.getZbInfoId();
        if (StringUtils.isNotBlank(zbInfoId)) {
            queryWrapper.eq(ZbModule::getZbInfoId, zbInfoId);
        }
        List<ZbModule> list = zbModuleService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getZbInfoById(@PathVariable String id) {
        ZbModule byId = zbModuleService.getById(id);
        return AjaxResult.success(byId);
    }

    /**
     * @param zbInfoId
     * @description: 根据装备id获取对应的zbModule列表
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-28 1:48
     */

    @GetMapping("/getZbModuleByZbInfo/{zbInfoId}")
    public AjaxResult getZbModuleByZbInfo(@PathVariable String zbInfoId) {
        List<ZbModule> zbModuleList = zbModuleService.list(Wrappers.<ZbModule>lambdaQuery()
                .eq(ZbModule::getZbInfoId, zbInfoId)
        );
        return AjaxResult.success(zbModuleList);
    }

    @PostMapping
    public AjaxResult addZbInfo(@RequestBody ZbModule zbModule) {
        return toAjax(zbModuleService.saveOrUpdate(zbModule));
    }

    @PutMapping
    public AjaxResult updateZbInfo(@RequestBody ZbModule zbModule) {
        return toAjax(zbModuleService.saveOrUpdate(zbModule));
    }

    @PutMapping("/setRelation")
    public AjaxResult setRelation(@RequestBody SetZbInfoAndZbModuleVo setZbInfoAndZbModuleVo) {
        zbModuleService.setZbInfoAndZbModule(setZbInfoAndZbModuleVo);
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteZbInfo(@PathVariable String id) {
        return toAjax(zbModuleService.removeById(id));
    }
}
